package cn.edu.bjtu.toychange.dao.support;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DataSourceSupport {
	private static DataSource dataSource;

	static {
		try {
			Context ctx = new InitialContext();
			dataSource = (DataSource) ctx
					.lookup("java:comp/env/jdbc/SPDatasource");
		} catch (NamingException e) {
			throw new RuntimeException("JNDI Connection Wrong!", e);
		}
	}

	/**
	 * 获取和数据库的连接
	 */
	public static Connection getConnection() {
		Connection con;
		try {
			con = dataSource.getConnection();
		} catch (SQLException e) {
			throw new RuntimeException("Connection Wrong!", e);
		}
		return con;
	}
}
